﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace SIMS.Company
{
    public class CompanyProfile
    {
        public CompanyProfile()
        {

        }

        public int Id { get; set; }
        public string CompanyName { get; set; }
        public string CompanyName2 { get; set; }
        public string ContactNoLine1 { get; set; }
        public string ContactNoLine2 { get; set; }
        public string FaxNoLine1 { get; set; }
        public string FaxNoLine2 { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string EmailAddress1 { get; set; }
        public string EmailAddress2 { get; set; }
        public string FounderName { get; set; }
        public string PrincipalName { get; set; }
        public Int16 OpeningYear { get; set; }
        public string Description1 { get; set; }
        public string Description2 { get; set; }
        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class CompanyProfileList : List<CompanyProfile>
    {
        public MyError Error { get; set; }
        public CompanyProfileList()
        {
        }
    }

    public class CompanyProfileDB
    {
        public CompanyProfileDB()
        {
        }

        MyDatabase _db = new MyDatabase();

        public CompanyProfile SaveProfile(CompanyProfile profile)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", profile.Id);

                if (profile.CompanyName2 != string.Empty)
                {
                    _param.Add("@CompanyName2", profile.CompanyName2);
                }
                else
                {
                    _param.Add("@CompanyName2", DBNull.Value);
                }

                if (profile.AddressLine1 != string.Empty)
                {
                    _param.Add("@AddressLine1", profile.AddressLine1);
                }
                else
                {
                    _param.Add("@AddressLine1", DBNull.Value);
                }

                if (profile.AddressLine2 != string.Empty)
                {
                    _param.Add("@AddressLine2", profile.AddressLine2);
                }
                else
                {
                    _param.Add("@AddressLine2", DBNull.Value);
                }

                _param.Add("@CompanyName", profile.CompanyName);

                if (profile.EmailAddress1 != string.Empty)
                {
                    _param.Add("@EmailAddress1", profile.EmailAddress1);
                }
                else
                {
                    _param.Add("@EmailAddress1", DBNull.Value);
                }

                if (profile.EmailAddress2 != string.Empty)
                {
                    _param.Add("@EmailAddress2", profile.EmailAddress2);
                }
                else
                {
                    _param.Add("@EmailAddress2", DBNull.Value);
                }

                if (profile.ContactNoLine1 != string.Empty)
                {
                    _param.Add("@ContactNoLine1", profile.ContactNoLine1);
                }
                else
                {
                    _param.Add("@ContactNoLine1", DBNull.Value);
                }

                if (profile.ContactNoLine2 != string.Empty)
                {
                    _param.Add("@ContactNoLine2", profile.ContactNoLine2);
                }
                else
                {
                    _param.Add("@ContactNoLine2", DBNull.Value);
                }

                if (profile.FaxNoLine1 != string.Empty)
                {
                    _param.Add("@FaxNoLine1", profile.FaxNoLine1);
                }
                else
                {
                    _param.Add("@FaxNoLine1", DBNull.Value);
                }

                if (profile.FaxNoLine2 != string.Empty)
                {
                    _param.Add("@FaxNoLine2", profile.FaxNoLine2);
                }
                else
                {
                    _param.Add("@FaxNoLine2", DBNull.Value);
                }

                if (profile.FounderName != string.Empty)
                {
                    _param.Add("@FounderName", profile.FounderName);
                }
                else
                {
                    _param.Add("@FounderName", DBNull.Value);
                }

                if (profile.PrincipalName != string.Empty)
                {
                    _param.Add("@PrincipalName", profile.PrincipalName);
                }
                else
                {
                    _param.Add("@PrincipalName", DBNull.Value);
                }

                if (profile.Description1 != string.Empty)
                {
                    _param.Add("@Description1", profile.Description1);
                }
                else
                {
                    _param.Add("@Description1", DBNull.Value);
                }

                if (profile.Description2 != string.Empty)
                {
                    _param.Add("@Description2", profile.Description2);
                }
                else
                {
                    _param.Add("@Description2", DBNull.Value);
                }

                if (profile.OpeningYear != 0)
                {
                    _param.Add("@OpeningYear", profile.OpeningYear);
                }
                else
                {
                    _param.Add("@OpeningYear", DBNull.Value);
                }

                _param.Add("@IsActive", profile.IsActive);

                int _returnValue = _db.ExecuteSP("usp_CompanyProfileInsertUpdateSingleItem", _param);

                if (_returnValue > 0)
                {
                    profile.Id = _returnValue;
                    profile.Error.ErrorNo = 0;
                }
                else
                {
                    profile.Error.ErrorNo = 1;
                    profile.Error.ErrorMessage = "No row affect when saving Company's profile details.";
                }
            }
            catch (Exception Ex)
            {
                profile.Error.ErrorNo = 10002;
                profile.Error.ErrorLocation = "CompanyProfile";
                profile.Error.ErrorMessage = Ex.Message;
                profile.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CompanyProfile");
            }

            return profile;
        }

        public CompanyProfile GetProfile(int Id)
        {
            CompanyProfile retValue = new CompanyProfile();

            try
            {
                DataTable _dt = new DataTable("tblCompanyProfile");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _dt = _db.GetDataTableBySP("usp_CompanyProfileSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "CompanyProfile";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CompanyProfile");
            }

            return retValue;
        }

        public DataTable GetProfileTable()
        {
            DataTable retValue = new DataTable();

            try
            {
                DataTable _dt = new DataTable("tblCompanyProfile");

                SortedList _param = new SortedList();
                _dt = _db.GetDataTableBySP("usp_CompanyProfileSelectItemList", _param);
                retValue = _dt;
            }
            catch (Exception Ex)
            {
                MyError.CreateExceptionLog(Ex, "CompanyProfile");
            }

            return retValue;
        }

        public CompanyProfileList GetProfileList()
        {
            CompanyProfileList retValue = new CompanyProfileList();

            try
            {
                DataTable _dt = new DataTable("tblCompanyProfile");

                SortedList _param = new SortedList();
                _dt = _db.GetDataTableBySP("usp_CompanyProfileSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        CompanyProfile _profile = new CompanyProfile();
                        FillData(_dr, _profile);
                        retValue.Add(_profile);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10003;
                retValue.Error.ErrorLocation = "CompanyProfile";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CompanyProfile");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, CompanyProfile aCompany)
        {
            aCompany.Id = Convert.ToInt32(aRow["Id"].ToString());
            aCompany.Error = new MyError();

            aCompany.CompanyName = aRow["CompanyName"].ToString();

            if (aRow["CompanyName2"] != DBNull.Value)
            {
                aCompany.CompanyName2 = aRow["CompanyName2"].ToString();
            }
            else
            {
                aCompany.CompanyName2 = string.Empty;
            }

            if (aRow["AddressLine1"] != DBNull.Value)
            {
                aCompany.AddressLine1 = aRow["AddressLine1"].ToString();
            }
            else
            {
                aCompany.AddressLine1 = string.Empty;
            }

            if (aRow["AddressLine2"] != DBNull.Value)
            {
                aCompany.AddressLine2 = aRow["AddressLine2"].ToString();
            }
            else
            {
                aCompany.AddressLine2 = string.Empty;
            }

            if (aRow["ContactNoLine1"] != DBNull.Value)
            {
                aCompany.ContactNoLine1 = aRow["ContactNoLine1"].ToString();
            }
            else
            {
                aCompany.ContactNoLine1 = string.Empty;
            }

            if (aRow["ContactNoLine2"] != DBNull.Value)
            {
                aCompany.ContactNoLine2 = aRow["ContactNoLine2"].ToString();
            }
            else
            {
                aCompany.ContactNoLine2 = string.Empty;
            }

            if (aRow["FaxNoLine1"] != DBNull.Value)
            {
                aCompany.FaxNoLine1 = aRow["FaxNoLine1"].ToString();
            }
            else
            {
                aCompany.FaxNoLine1 = string.Empty;
            }

            if (aRow["FaxNoLine2"] != DBNull.Value)
            {
                aCompany.FaxNoLine2 = aRow["FaxNoLine2"].ToString();
            }
            else
            {
                aCompany.FaxNoLine2 = string.Empty;
            }

            if (aRow["EmailAddress1"] != DBNull.Value)
            {
                aCompany.EmailAddress1 = aRow["EmailAddress1"].ToString();
            }
            else
            {
                aCompany.EmailAddress1 = string.Empty;
            }

            if (aRow["EmailAddress2"] != DBNull.Value)
            {
                aCompany.EmailAddress2 = aRow["EmailAddress2"].ToString();
            }
            else
            {
                aCompany.EmailAddress2 = string.Empty;
            }

            if (aRow["Description1"] != DBNull.Value)
            {
                aCompany.Description1 = aRow["Description1"].ToString();
            }
            else
            {
                aCompany.Description1 = string.Empty;
            }

            if (aRow["Description2"] != DBNull.Value)
            {
                aCompany.Description2 = aRow["Description2"].ToString();
            }
            else
            {
                aCompany.Description2 = string.Empty;
            }

            if (aRow["OpeningYear"] != DBNull.Value)
            {
                aCompany.OpeningYear = Convert.ToInt16(aRow["OpeningYear"].ToString());
            }
            else
            {
                aCompany.OpeningYear = 0;
            }

            if (aRow["FounderName"] != DBNull.Value)
            {
                aCompany.FounderName = aRow["FounderName"].ToString();
            }
            else
            {
                aCompany.FounderName = string.Empty;
            }

            if (aRow["PrincipalName"] != DBNull.Value)
            {
                aCompany.PrincipalName = aRow["PrincipalName"].ToString();
            }
            else
            {
                aCompany.PrincipalName = string.Empty;
            }

            aCompany.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            aCompany.Error.ErrorNo = 0;
        }
    }
}
